package com.library.uiframework.activity;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.library.uiframework.Immersive.ImmersiveHelper;
import com.library.uiframework.Immersive.ImmersiveObserver;
import com.library.uiframework.Immersive.ImmersiveOnApplyStyleListener;
import com.library.uiframework.Immersive.ImmersiveOnObserverInitedListener;
import com.library.uiframework.R;
import com.library.uiframework.slidingclose.SlideHelperSlidingCloseInterface;
import com.library.uiframework.slidingclose.SlidingClosableRelativeLayout;
import com.library.uiframework.slidingclose.SlidingCloseableHelper;
import com.library.uiframework.utils.DisplayUtils;
import com.library.uiframework.widget.actionbar.ActionBarLayout;
import com.library.uiframework.widget.actionbar.ActionBarLayout.OnActionClickListener;
import com.library.uiframework.widget.actionbar.ActionMenu;
import com.library.uiframework.widget.actionbar.ActionMenuItem;
import com.library.uiframework.widget.state.StateView;

/**
 * @author hongkai.qian
 * @version 1.0.0
 * @since 16-1-8
 */
public abstract class ActionBarLayoutActivity extends BaseActivity
    implements ImmersiveOnApplyStyleListener, ImmersiveOnObserverInitedListener
    , SlideHelperSlidingCloseInterface {

    /**
     * 后退按钮
     */
    protected static final int ACTION_BACK = -1;
    /**
     * 菜单按钮
     */
    protected static final int ACTION_MENU = -2;

    private ActionBarLayout mActionBarLayout;
    private ViewGroup mBodyContainer;
    private View mViewShadow;

    private ImmersiveHelper mImmersiveHelper;

    private ActionBarLayout.Action mActionBack;
    private ActionBarLayout.Action mActionMenu;

    private SlidingCloseableHelper mSlidingCloseableHelper = new SlidingCloseableHelper();

    private ActionBarLayout.OnActionClickListener mOnActionClickListener = new ActionBarLayout.OnActionClickListener() {
        @Override
        public void onClick(int actionId, ActionBarLayout.Action action) {
            if (action == mActionBack) {
                onBackPressed();
            } else {
                onActionClick(actionId, action);
            }
        }
    };
    private ActionMenu mPopupMenu;

    public OnActionClickListener getOnActionClickListener() {
        return mOnActionClickListener;
    }

    /**
     * @return back action button
     */
    public ActionBarLayout.Action getActionBack() {
        return mActionBack;
    }

    /**
     * @return menu action button
     */
    public ActionBarLayout.Action getActionMenu() {
        return mActionMenu;
    }

    /**
     * ------------------------  子类可能需要根据情况重载的方法   -------------------------
     **/

    protected abstract View onCreateContentView(LayoutInflater inflater, ViewGroup container,
                                                Bundle savedInstanceState);

    /**
     * 提供给子类添加自定义Action和初始化标题之类的操作
     */
    protected void onInitActionBar() {
        this.mActionBarLayout.setMode(ActionBarLayout.MODE_TEXT);
    }

    /**
     * on click action button
     *
     * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param action   action action 也是告知点的哪个按钮
     */
    protected void onActionClick(int actionId, ActionBarLayout.Action action) {
        if (actionId == ACTION_MENU) {
            List<ActionMenuItem> menus = new ArrayList<>();
            beforeCreatePopupMenu(menus);
            onCreatePopupMenu(menus);
            if (menus.size() > 0) {
                mPopupMenu = ActionMenu.getInstance(this, menus, new ActionMenu.ActionMenuCallback() {
                    @Override
                    public boolean onSelect(View view, ActionMenuItem actionMenuItem, int position) {
                        return onPopupMenuSelected(view, actionMenuItem, position);
                    }
                });

                mPopupMenu.enableShadowBg(getWindow());
                mPopupMenu.show(action.getActionView(), Gravity.RIGHT | Gravity.TOP, DisplayUtils.dp2px(10),
                    (int)(DisplayUtils.dp2px(54) + DisplayUtils.getStatusBarHeight()));
            }
        }
    }

    /**
     * beforeCreatePopupMenu
     *
     * @param menus menus
     */
    protected void beforeCreatePopupMenu(List<ActionMenuItem> menus) {
    }

    /**
     * onPopupMenuSelected
     *
     * @param view     view
     * @param menuItem menuItem
     * @param position position
     * @return
     */
    protected boolean onPopupMenuSelected(View view, ActionMenuItem menuItem, int position) {

        return false;
    }

    /**
     * 创建溢出menu
     *
     * @param menus menus
     */
    protected void onCreatePopupMenu(List<ActionMenuItem> menus) {
    }

    /**
     * @return 是否需要后退按钮, 默认需要
     */
    protected boolean needBackAction() {
        return true;
    }

    /**
     * @return 是否需要菜单按钮, 3个横向的点
     */
    protected boolean needMenuAction() {
        return false;
    }

    @Override
    public boolean needApplyStatusBarStyle() {
        return true;
    }

    @Override
    public boolean needApplyNavigationBarStyle() {
        return true;
    }

    /**
     * ------------------------  子类可能需要根据情况重载的方法  end   -------------------------
     **/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLaunchFragmentAttr(R.id.actionbar_container, R.anim.page_slide_in_right, R.anim.page_slide_out_right);
        final View contentView = buildContentView(LayoutInflater.from(this), null, savedInstanceState);
        setContentView(mSlidingCloseableHelper.injectView(contentView));
    }

    View buildContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mainView = inflater.inflate(commonActionBarLayoutId(), container, false);
        setBusinessView(mainView);
        mActionBarLayout = (ActionBarLayout)mainView.findViewById(R.id.actionbar_layout);
        mViewShadow = mainView.findViewById(R.id.actionbar_shadow);
        mBodyContainer = (ViewGroup)mainView.findViewById(R.id.actionbar_container);
        if (mBodyContainer instanceof StateView) {
            initStateViewContainer(inflater, (StateView)mBodyContainer, savedInstanceState);
        } else {
            final View contentView = onCreateContentView(inflater, mBodyContainer, savedInstanceState);
            if (contentView != null) {
                mBodyContainer.addView(contentView);
            }
        }
        if (needBackAction()) {
            mActionBack = createBack();
        }

        if (needMenuAction()) {
            mActionMenu = createMenu();
        }

        onInitActionBar();
        return mainView;
    }

    protected ActionBarLayout.Action createMenu() {
        return addIconTextAction(ACTION_MENU, R.string.ic_quanjugengduo);
    }

    protected ActionBarLayout.Action createBack() {
        return addLeftIconTextAction(ACTION_BACK, R.string.ic_quanjufanhui);
    }

    int commonActionBarLayoutId() {
        return R.layout.uiframework_actionbar_layout;
    }

    void initStateViewContainer(LayoutInflater inflater, StateView stateView, Bundle savedInstanceState) {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersiveHelper != null) {
            mImmersiveHelper.onDestroyView();
        }
        if (mSlidingCloseableHelper != null && mSlidingCloseableHelper.getSlidingClosableRelativeLayout() != null) {
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setVisibility(View.GONE);
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setOnSlidingCloseListener(null);
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setOnSlidingScrollListener(null);
            mSlidingCloseableHelper = null;
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mImmersiveHelper = new ImmersiveHelper(this);
        mImmersiveHelper.applyStyle(this, this);
        mSlidingCloseableHelper.setSlideHelperSlidingCloseInterface(this);
    }

    @Override
    public void onInitImmersiveObserver(ImmersiveObserver immersiveObserver) {
        final View view = getWindow() != null ? getWindow().getDecorView() : null;
        if (view != null) {
            immersiveObserver.setImmersiveView(mActionBarLayout, mBodyContainer
                , view.findViewById(R.id.status_bar_background), view.findViewById(R.id.navigate_bar_background));
        }
    }

    public void setStatusBarColor(@ColorInt int color) {
        View view = getWindow().getDecorView().findViewById(R.id.status_bar_background);
        if (view != null) {
            view.setBackgroundColor(color);
        }
    }

    public void setNavigateBarColor(@ColorInt int color) {
        View view =getWindow().getDecorView().findViewById(R.id.navigate_bar_background);
        if (view != null) {
            view.setBackgroundColor(color);
        }
    }

    /**
     * @return action bar layout
     */
    public ActionBarLayout getActionBarLayout() {
        return mActionBarLayout;
    }

    /**
     * @param title 设置工具栏标题
     */
    public void setTitle(String title) {
        mActionBarLayout.setTitle(title);
    }

    /**
     * @param titleResId 设置工具栏标题
     */
    @Override
    public void setTitle(int titleResId) {
        mActionBarLayout.setTitle(titleResId);
    }

    /**
     * @param color 设置工具栏标题颜色
     */
    @Override
    public void setTitleColor(int color) {
        mActionBarLayout.setTitleColor(color);
    }

    /**
     * 右侧增加一个图片样式的动作按钮
     *
     * @param actionId   actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param imageResId resource id
     * @return action
     */
    protected ActionBarLayout.Action addImageAction(int actionId, int imageResId) {
        ActionBarLayout.Action action = mActionBarLayout.addImageAction(actionId, imageResId);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 右侧增加一个文字样式的动作按钮
     *
     * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param txtResId resource id
     * @return action
     */
    protected ActionBarLayout.Action addTextAction(int actionId, int txtResId) {
        ActionBarLayout.Action action = mActionBarLayout.addTextAction(actionId, txtResId);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 右侧增加一个文字样式的动作按钮
     *
     * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param text     text string
     * @return action
     */
    protected ActionBarLayout.Action addTextAction(int actionId, String text) {
        ActionBarLayout.Action action = mActionBarLayout.addTextAction(actionId, text);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 右侧增加一个字体图标文字样式的动作按钮
     *
     * @param actionId      actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param iconTextResId resource id
     * @return action
     */
    protected ActionBarLayout.Action addIconTextAction(int actionId, int iconTextResId) {
        ActionBarLayout.Action action = mActionBarLayout.addIconTextAction(actionId, iconTextResId, null);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 右侧增加一个图片样式的动作按钮
     *
     * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param drawable drawable形式的图片
     * @return action
     */
    protected ActionBarLayout.Action addImageAction(int actionId, Drawable drawable) {
        ActionBarLayout.Action action = mActionBarLayout.addImageAction(actionId, drawable);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 在左侧增加一个图片样式的动作按钮
     *
     * @param actionId   actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param imageResId resource id
     * @return action
     */
    protected ActionBarLayout.Action addLeftImageAction(int actionId, int imageResId) {
        ActionBarLayout.Action action = mActionBarLayout.addLeftImageAction(actionId, imageResId);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 在左侧增加一个文字样式的动作按钮
     *
     * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param txtResId resource id
     * @return action
     */
    protected ActionBarLayout.Action addLeftTextAction(int actionId, int txtResId) {
        ActionBarLayout.Action action = mActionBarLayout.addLeftTextAction(actionId, txtResId);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 在左侧增加一个字体图标文字样式的动作按钮
     *
     * @param actionId      actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param iconTextResId resource id
     * @return action
     */
    protected ActionBarLayout.Action addLeftIconTextAction(int actionId, int iconTextResId) {
        ActionBarLayout.Action action = mActionBarLayout.addLeftIconTextAction(actionId, iconTextResId, null);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * 在左侧增加一个图片样式的动作按钮
     *
     * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
     * @param drawable drawable形式的图片
     * @return action
     */
    protected ActionBarLayout.Action addLeftImageAction(int actionId, Drawable drawable) {
        ActionBarLayout.Action action = mActionBarLayout.addLeftImageAction(actionId, drawable);
        action.setOnActionClickListener(mOnActionClickListener);
        return action;
    }

    /**
     * @param floatingMode 顶部工具栏是否使用浮动模式
     */
    public void setFloatingMode(boolean floatingMode) {
        final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mBodyContainer.getLayoutParams();
        if (floatingMode) {
            layoutParams.addRule(RelativeLayout.BELOW, 0);
        } else {
            layoutParams.addRule(RelativeLayout.BELOW, R.id.actionbar_layout);
        }
        mBodyContainer.setLayoutParams(layoutParams);
    }

    /**
     * @return 顶部工具栏是否是浮动模式
     */
    public boolean isFloatingMode() {
        final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mBodyContainer.getLayoutParams();
        return layoutParams.getRules()[RelativeLayout.BELOW] == 0;
    }

    /**
     * @param visibility 顶部工具栏下面的阴影显示隐藏
     */
    public void setShadowVisiblity(int visibility) {
        mViewShadow.setVisibility(visibility);
    }

    @Override
    public void onSlidingClosed() {
        finish();
    }

    /**
     * @return SlidingClosableRelativeLayout
     */
    public SlidingClosableRelativeLayout getSlidingContainer() {
        if (mSlidingCloseableHelper != null) {
            return mSlidingCloseableHelper.getSlidingClosableRelativeLayout();
        }
        return null;
    }

    /**
     * 设置滑动关闭的模式
     *
     * @param closeMode 滑动关闭的模式
     */
    public void setSlidingCloseMode(int closeMode) {
        if (mSlidingCloseableHelper != null && mSlidingCloseableHelper.getSlidingClosableRelativeLayout() != null) {
            mSlidingCloseableHelper.getSlidingClosableRelativeLayout().setSlidingCloseMode(closeMode);
        }
    }
}
